/**
 * Copyright (C), 2019-2022, 宇信融汇
 * FileName: Sorter
 * Author:   666
 * Date:     2022/4/27 15:02
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 666           修改时间           版本号              描述
 */
package com.strategy;

/**
 * @Classname Sorter
 * @Description 排序
 * @Date 2022/4/27 15:02
 * @Created by 666
 */
public class Sorter<T> {
    public void sort(T[] arr, Comparator<T> comparator) {
        for (int i = 0; i < arr.length - 1; i++) {
            int minPos = i;
            for (int j = i + 1; j < arr.length; j++) {
                minPos = comparator.compare(arr[j], arr[minPos]) == -1 ? j : minPos;
            }
            swap(arr, i, minPos);
        }

    }

    private void swap(T[] arr, int i, int j) {
        T temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}
